#include "EvalBleedThroughIntensity2Ink.hpp"
#include "ThresholderOtsuBleedThroughAware.hpp"

/** @todo cleanup */
float EvalBleedThroughIntensity2Ink::evaluate(const girl::tools::Image &image)
{

    float s=0;
    float nb =0;

    float sum_ink = 0;
    float nb_ink = 0;
    ThresholderOtsuBleedThroughAware _thresholder;
    int seuil_verso = _thresholder.getVersoTreshold(_verso);
    int seuil_recto = _thresholder.getRectoThreshold(image,_verso,seuil_verso);
    seuil_verso = seuil_recto;

    std::cout << "recto" << seuil_recto << std::endl;
    std::cout << "verso" << seuil_verso << std::endl;

    for(int i = 0; i < image.width(); i++)
    {
        for (int j= 0; j  < image.height(); j++)
        {
            if(_verso.getPixel(i,j).a() < seuil_verso)
            {
                s+=image.getPixel(i,j).a();
                nb++;
            }
            if(image.getPixel(i,j).a() < seuil_recto)
            {
                sum_ink+=image.getPixel(i,j).a();
                nb_ink++;
            }
        }
    }
    float moyenne_ink = seuil_recto;
    float moyenne_bleed = s/nb;
    if(nb_ink > 5*nb/100)
        moyenne_ink = sum_ink / nb_ink;



    std::cout << "moyenne ink" << moyenne_ink << std::endl;
    std::cout << "moyenne bleed" << moyenne_bleed << std::endl;

    return (moyenne_bleed - moyenne_ink) / 255 ;
}



/** @todo consider deprecated */
float EvalBleedThroughIntensity2Ink::ndgMoyen(const girl::regionDeque &regions, const girl::toger &tog)
{

    girl::tools::Pixel red(255,0,0);

    girl::regionDeque::const_iterator it = regions.begin();
    girl::regionDeque::const_iterator itEnd = regions.end();

    int s=0;
    int nb=0;
    for(; it != itEnd; ++it)
    {
        std::vector<girl::point> points;
        tog.regionDomain(*it,points);
        for(unsigned int i= 0; i < points.size() ; i++)
        {
            //ENLEVER !
            //s+= _image->getPixel(points[i].x(),points[i].y()).a();

            nb++;
        }
    }

    if(nb>0) return s/nb;
    else return 0;
}
